/*
 * Copyright 2016-2036 the original author or authors.
 * 
 * COMMERCIAL USE OF THIS SOFTWARE WITHOUT WARRANTY IS NOT ALLOWED.
 * Use is subject to license terms! You can distribute a copy of this software
 * to others for free. This software is a non-profit and open-source project.
 * Any contribution to this project will make it better.
 * All rights reserved! Owned by Stephen Liu.
 * 
 */
package com.github.avatar.config.dubbo;

import com.alibaba.dubbo.config.ReferenceConfig;
import com.alibaba.dubbo.config.spring.ReferenceBean;
import com.github.avatar.service.HelloService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * Refer remote service into local.
 * @author Stephen Liu
 * @since 2017/6/27
 */
@Configuration
@EnableConfigurationProperties(value = Consumer.class)
public class ReferServiceConfig extends DubboBaseConfig {

    @Autowired
    private Consumer consumer;

    @Bean
    public ReferenceConfig referenceConfig() {
        ReferenceConfig referenceConfig = new ReferenceConfig();
        referenceConfig.setMonitor(monitor());
        referenceConfig.setMock(true);
        return referenceConfig;
    }

    /**
     * Actual remote service.
     * @return
     */
    @Bean
    public ReferenceBean<HelloService> universityService() {
        ReferenceBean<HelloService> refer = new ReferenceBean<>();
        refer.setInterface(HelloService.class);
        refer.setVersion(consumer.getVersion());
        refer.setTimeout(consumer.getTimeout());
        refer.setCheck(true);
        refer.setRetries(consumer.getRetries());
        return refer;
    }
}
